/*
 * 文 件 名:  DispatchAction.java
 * 版    权:  Tydic Technologies Co., Ltd. Copyright 1993-2013,  All rights reserved
 * 描    述:  <描述>
 * 修 改 人:  yanglong
 * 修改时间:  2013-11-10
 * 跟踪单号:  <需求跟踪单号>
 * 修改单号:  <需求修改单号>
 * 修改内容:  <修改内容>
 */
package com.hfut.jpoa.action;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.ServletActionContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.hfut.jpoa.common.action.AbsAction;
import com.hfut.jpoa.common.dto.Pagination;
import com.hfut.jpoa.common.dto.ResultDto;
import com.hfut.jpoa.common.util.ConstantUtil;
import com.hfut.jpoa.dto.NoticeDTO;
import com.hfut.jpoa.model.Dispatch;
import com.hfut.jpoa.model.Notice;
import com.hfut.jpoa.model.Syspara;
import com.hfut.jpoa.model.User;
import com.hfut.jpoa.service.CustomerService;
import com.hfut.jpoa.service.DispatchService;
import com.hfut.jpoa.service.NoticeService;
import com.hfut.jpoa.service.SysparaService;
import com.hfut.jpoa.service.UserService;
import com.hfut.jpoa.service.exception.NoticeException;
import com.hfut.jpoa.service.exception.SysparaException;
import com.hfut.jpoa.service.exception.UserException;

/**
 * 活动提醒
 * 
 * @author yanglong
 * @version [1.0, 2013-11-10]
 * @since [产品/1.0]
 */
public class NoticeAction extends AbsAction<NoticeDTO>
{

	private static final Logger LOGGER = LoggerFactory
	        .getLogger(NoticeAction.class);

	private static final long serialVersionUID = -7819502638545092791L;

	private DispatchService dispatchService;

	private Dispatch dispatch;

	private List<Dispatch> lstDispatchs;

	/**
	 * 标志位：0-增加 1-修改 2-详情
	 */
	private String actionType;

	/**
	 * 页面类型：0：调度中心 1：售后部
	 */
	private String pageType;

	/**
	 * 包含分页接受前台变量
	 */
	private NoticeDTO noticeDTO;

	/**
	 * 分页结果集
	 */
	private ResultDto<Notice> resultDto;

	/**
	 * 总页数
	 */
	private int totalPage;

	/**
	 * 用户列表
	 */
	private List<User> lstUsers;

	/**
	 * 创建者,以后从session中取
	 */
	private String creator = "yl";

	/**
	 * 用户服务
	 */
	private UserService userService;

	/**
	 * 字典服务
	 */
	private SysparaService sysparaService;

	/**
	 * 客户服务
	 */
	private CustomerService customerService;

	/**
	 * 字典服务
	 */
	private NoticeService noticeService;

	/**
	 * 路线列表
	 */
	private List<Syspara> lstDepart;

	/**
	 * 通知提醒
	 */
	private Notice notice;

	/**
	 * 加载选择项目弹出层
	 * 
	 * @return
	 */
	public String initDialog() throws NoticeException
	{
		initDate();
		return "init_dialog_list";
	}

	public String listDialog() throws NoticeException
	{

		HttpServletRequest request = ServletActionContext.getRequest();
		String currentPage = request.getParameter("currentPage") == null ? ""
		        : request.getParameter("currentPage");
		Pagination pagination = new Pagination(Integer.parseInt(currentPage));
		String pagesize = request.getParameter("pageSize");
		pagination.setPageSize(Integer.parseInt(pagesize));
		if (noticeDTO == null)
		{
			noticeDTO = new NoticeDTO();
		}
		noticeDTO.setPagination(pagination);
		resultDto = noticeService.queryInfoForPage(noticeDTO);

		totalPage = resultDto.getPagination().getTotalPage();

		return "dispatch_dialog_list";
	}

	/**
	 * 初始化部门列表
	 * 
	 * @return
	 * @throws NoticeException
	 */
	private List<Syspara> findLstDepart() throws NoticeException
	{
		// 从字典表中加载初始化部门列表
		List<Syspara> lstDepartTemp = new ArrayList<Syspara>();
		try
		{
			Syspara syspara = new Syspara();
			syspara.setType(ConstantUtil.DEPARTMENT);
			lstDepartTemp = sysparaService.querySysparaList(syspara);
		} catch (SysparaException e)
		{
			LOGGER.error("初始化部门列表异常：", e);
			throw new NoticeException("初始化部门列表异常：", e);
		}
		return lstDepartTemp;
	}

	/**
	 * 加载列表页显示数据
	 */
	private void initDate() throws NoticeException
	{

		NoticeDTO dto = new NoticeDTO();
		Pagination pagination = new Pagination(0);
		dto.setPagination(pagination);
		resultDto = noticeService.queryInfoForPage(dto);
		totalPage = resultDto.getPagination().getTotalPage();
	}

	/**
	 * 加载分页列表
	 * 
	 * @return
	 * @throws NoticeException
	 */
	public String listInit() throws NoticeException
	{
		// 跳转页面
		return "list_init";
	}

	public String toCenterList() throws NoticeException
	{
		listDate();
		actionType = ConstantUtil.ACTIONTYPE_CENTER;
		return "list";
	}

	/**
	 * 加载列表数据
	 * 
	 * @return
	 */
	public String list() throws NoticeException
	{
		listDate();
		actionType = ConstantUtil.ACTIONTYPE_LIST;
		return "list";
	}

	private void listDate() throws NoticeException
	{

		HttpServletRequest request = ServletActionContext.getRequest();
		String currentPage = request.getParameter("currentPage");
		Pagination pagination = new Pagination(Integer.parseInt(currentPage));
		String pagesize = request.getParameter("pageSize");
		pagination.setPageSize(Integer.parseInt(pagesize));
		if (noticeDTO == null)
		{
			noticeDTO = new NoticeDTO();
		}
		noticeDTO.setPagination(pagination);
		resultDto = noticeService.queryInfoForPage(noticeDTO);

		List<Notice> result = resultDto.getResult();

		// 加载用户列表
		List<User> initLstUser = initLstUser();
		// 加载部门列表
		List<Syspara> lstDepart = findLstDepart();

		for (int i = 0; i < result.size(); i++)
		{
			// 用户名
			for (int j = 0; j < initLstUser.size(); j++)
			{
				if (StringUtils.equals(result.get(i).getCreatePeople(),
				        initLstUser.get(j).getUserCode()))
				{
					result.get(i).setCreatePeopleName(
					        initLstUser.get(j).getName());
				}
				if (StringUtils.equals(result.get(i).getReceivePeople(),
				        initLstUser.get(j).getUserCode()))
				{
					result.get(i).setReceivePeopleName(
					        initLstUser.get(j).getName());
				}

			}
			// 部门名称
			for (int j = 0; j < lstDepart.size(); j++)
			{
				if (StringUtils.equals(result.get(i).getDepartCode(), lstDepart
				        .get(j).getCode()))
				{
					result.get(i).setDepartName(lstDepart.get(j).getValue());
				}
				if (StringUtils.equals(result.get(i).getReceiveDepart(),
				        lstDepart.get(j).getCode()))
				{
					result.get(i).setReceiveDepartName(
					        lstDepart.get(j).getValue());
				}
			}

		}

		totalPage = resultDto.getPagination().getTotalPage();

	}

	/**
	 * 加载用户列表
	 * 
	 * @param user
	 * @return
	 * @throws NoticeException
	 */
	public List<User> initLstUser() throws NoticeException
	{
		// 加载员工

		User user = new User();

		List<User> lstUser;
		try
		{
			lstUser = userService.queryUser(user);
		} catch (UserException e)
		{
			LOGGER.error("加载员工异常：", e);
			throw new NoticeException("加载员工异常：", e);
		}
		return lstUser;
	}

	/**
	 * 新增页面初始化
	 * 
	 * @return
	 */
	public String addInit() throws NoticeException
	{
		actionType = ConstantUtil.ACTIONTYPE_ADD;
		return "add_init";
	}

	/**
	 * 新增保存
	 * 
	 * @return
	 */
	public String add() throws NoticeException
	{

		noticeService.saveNotice(notice);

		return "list_init";
	}

	/**
	 * 管理中处理页面初始化
	 */
	public String processInit() throws NoticeException
	{

		actionType = ConstantUtil.ACTIONTYPE_CENTER;
		Notice resultDispatch = noticeService.findInfo(notice);
		notice = resultDispatch;

		return "add_init";
	}

	/**
	 * 修改提醒状态
	 * 
	 * @return
	 * @throws NoticeException
	 * @throws IOException
	 */
	public void processNotice() throws NoticeException, IOException
	{
		if (null == notice)
		{
			failure("系统异常，请联系管理员！");
		}
		notice.setResult("1");
		noticeService.update(notice);
		success("操作成功！");
	}

	/**
	 * 修改页面初始化
	 */
	public String updateInit() throws NoticeException
	{

		actionType = ConstantUtil.ACTIONTYPE_UPDATE;
		Notice resultDispatch = noticeService.findInfo(notice);
		notice = resultDispatch;

		return "add_init";
	}

	/**
	 * 修改字典表
	 * 
	 * @return
	 * @throws NoticeException
	 */
	public String updateDispatch() throws NoticeException
	{

		noticeService.update(notice);
		return "dispatch_list_init";
	}

	/**
	 * 修改
	 */
	public void updateState()
	{
		/*
		 * try { // dispatchService.updateDispatch(dispatch); } catch
		 * (NoticeException e1) { try { failure("操作异常：" + e1.getMessage()); }
		 * catch (IOException e) { LOGGER.error("修改字典配置异常", e); } } try {
		 * success("成功操作！"); } catch (IOException e) { LOGGER.error("修改字典配置异常",
		 * e); }
		 */
	}

	/**
	 * 验证编码是否存在
	 * 
	 * @throws IOException
	 */
	public void codeValidator() throws IOException
	{
		/*
		 * boolean flag = false; try { flag =
		 * dispatchService.codeValidator(dispatch); } catch (NoticeException e)
		 * { LOGGER.error("验证编码是否存在异常", e); failure("验证编码是否存在异常" +
		 * e.getMessage()); } if (flag) { failure("该编码已存在，请重新输入"); } else {
		 * success("输入正确，该编码可以使用"); }
		 */
	}

	public String getActionType()
	{
		return actionType;
	}

	public void setActionType(String actionType)
	{
		this.actionType = actionType;
	}

	public DispatchService getDispatchService()
	{
		return dispatchService;
	}

	public void setDispatchService(DispatchService dispatchService)
	{
		this.dispatchService = dispatchService;
	}

	public Dispatch getDispatch()
	{
		return dispatch;
	}

	public void setDispatch(Dispatch dispatch)
	{
		this.dispatch = dispatch;
	}

	public List<Dispatch> getLstDispatchs()
	{
		return lstDispatchs;
	}

	public void setLstDispatchs(List<Dispatch> lstDispatchs)
	{
		this.lstDispatchs = lstDispatchs;
	}

	public int getTotalPage()
	{
		return totalPage;
	}

	public void setTotalPage(int totalPage)
	{
		this.totalPage = totalPage;
	}

	public UserService getUserService()
	{
		return userService;
	}

	public void setUserService(UserService userService)
	{
		this.userService = userService;
	}

	public List<User> getLstUsers()
	{
		try
		{
			lstUsers = initLstUser();
		} catch (NoticeException e)
		{
			LOGGER.error("查询用户异常：", e);
		}
		return lstUsers;
	}

	public void setLstUsers(List<User> lstUsers)
	{
		this.lstUsers = lstUsers;
	}

	public String getPageType()
	{
		return pageType;
	}

	public void setPageType(String pageType)
	{
		this.pageType = pageType;
	}

	public CustomerService getCustomerService()
	{
		return customerService;
	}

	public void setCustomerService(CustomerService customerService)
	{
		this.customerService = customerService;
	}

	public NoticeDTO getNoticeDTO()
	{
		return noticeDTO;
	}

	public void setNoticeDTO(NoticeDTO noticeDTO)
	{
		this.noticeDTO = noticeDTO;
	}

	public NoticeService getNoticeService()
	{
		return noticeService;
	}

	public void setNoticeService(NoticeService noticeService)
	{
		this.noticeService = noticeService;
	}

	public List<Syspara> getLstDepart()
	{
		try
		{
			lstDepart = findLstDepart();
		} catch (NoticeException e)
		{
			LOGGER.error("查询部门异常：", e);
		}
		return lstDepart;
	}

	public void setLstDepart(List<Syspara> lstDepart)
	{
		this.lstDepart = lstDepart;
	}

	public void setResultDto(ResultDto<Notice> resultDto)
	{
		this.resultDto = resultDto;
	}

	public SysparaService getSysparaService()
	{
		return sysparaService;
	}

	public void setSysparaService(SysparaService sysparaService)
	{
		this.sysparaService = sysparaService;
	}

	public ResultDto<Notice> getResultDto()
	{
		return resultDto;
	}

	public Notice getNotice()
	{
		return notice;
	}

	public void setNotice(Notice notice)
	{
		this.notice = notice;
	}

}
